package smartcarpool.logic;

import android.widget.EditText;
import com.example.smartcarpool.R;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.Spinner;
import smartcarpool.smartspaceservice.Triple;
import smartcarpool.smartspaceservice.TripleList;

import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: tnn
 * Date: 20.05.13
 * Time: 18:11
 * To change this template use File | Settings | File Templates.
 */
public class SmartSpaceUserProfile implements Parcelable{

    public String user_id = "";
    public String user_name = "";
    public String user_type = "";
    public String user_vehicle = "";
    public String vacant_seats = "";
    public String vacant_items = "";
    public String user_preference = "";
    public String start_point_x = "";
    public String start_point_y = "";
    public String start_point_date = "";
    public String start_point_time = "";
    public String end_point_x = "";
    public String end_point_y = "";
    public String end_point_date = "";
    public String end_point_time = "";


    public SmartSpaceUserProfile (FragmentManager fragmentManager){
        user_id = "android_user1";
        Log.d("SmartSpaceService", "Start collecting information");
        Fragment fr1 = fragmentManager.findFragmentByTag("android:switcher:"+ R.id.pager+":0");
        Fragment fr2 = fragmentManager.findFragmentByTag("android:switcher:"+R.id.pager+":1");
        //Status
        Spinner sp1 = (Spinner) fr1.getView().findViewById(R.id.spinner1);
        user_type = sp1.getSelectedItem().toString();
        if (sp1.getSelectedItemPosition() == 0){
            //Vehicle type
            Spinner sp2 = (Spinner) fr1.getView().findViewById(R.id.spinner2);
            user_vehicle = sp2.getSelectedItem().toString();
            Log.d("SmartSpaceService user_type", user_vehicle);
            EditText eText_seats = (EditText) fr1.getView().findViewById(R.id.editText1);
            vacant_seats = eText_seats.getText().toString();
            EditText eText_places = (EditText) fr1.getView().findViewById(R.id.editText2);
            vacant_items = eText_places.getText().toString();
        }
        Spinner sp3 = (Spinner) fr1.getView().findViewById(R.id.spinner3);
        user_preference = sp3.getSelectedItem().toString();
        EditText eText_startPoint = (EditText) fr2.getView().findViewById(R.id.editText1);
        String[] start_coords = eText_startPoint.getText().toString().split(";");
        start_point_x = start_coords[0];//TODO: replace with geocoding function
        start_point_y = start_coords[1];
        EditText eText_startDate = (EditText) fr2.getView().findViewById(R.id.editText3);
        start_point_date = eText_startDate.getText().toString();
        EditText eText_startTime = (EditText) fr2.getView().findViewById(R.id.editText4);
        start_point_time = eText_startTime.getText().toString();
        EditText eText_endPoint = (EditText) fr2.getView().findViewById(R.id.editText2);
        String[] end_coords = eText_endPoint.getText().toString().split(";");
        end_point_x = end_coords[0];//TODO: replace with geocoding function
        end_point_y = end_coords[1];
        EditText eText_endDate = (EditText) fr2.getView().findViewById(R.id.editText5);
        end_point_date = eText_endDate.getText().toString();
        EditText eText_endTime = (EditText) fr2.getView().findViewById(R.id.editText6);
        end_point_time = eText_endTime.getText().toString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(user_id);
        dest.writeString(user_name);
        dest.writeString(user_type);
        dest.writeString(user_vehicle);
        dest.writeString(vacant_seats);
        dest.writeString(vacant_items);
        dest.writeString(user_preference);
        dest.writeString(start_point_x);
        dest.writeString(start_point_y);
        dest.writeString(start_point_date);
        dest.writeString(start_point_time);
        dest.writeString(end_point_x);
        dest.writeString(end_point_y);
        dest.writeString(end_point_date);
        dest.writeString(end_point_time);
    }

    public static final Parcelable.Creator<SmartSpaceUserProfile> CREATOR
            = new Parcelable.Creator<SmartSpaceUserProfile>() {
        public SmartSpaceUserProfile createFromParcel(Parcel in) {
            return new SmartSpaceUserProfile(in);
        }

        public SmartSpaceUserProfile[] newArray(int size) {
            return new SmartSpaceUserProfile[size];
        }
    };

    private SmartSpaceUserProfile(Parcel parcel){
        this.user_id = parcel.readString();
        this.user_name = parcel.readString();
        this.user_type = parcel.readString();
        this.user_vehicle = parcel.readString();
        this.vacant_seats = parcel.readString();
        this.vacant_items = parcel.readString();
        this.user_preference = parcel.readString();
        this.start_point_x = parcel.readString();
        this.start_point_y = parcel.readString();
        this.start_point_date = parcel.readString();
        this.start_point_time = parcel.readString();
        this.end_point_x = parcel.readString();
        this.end_point_y = parcel.readString();
        this.end_point_date = parcel.readString();
        this.end_point_time = parcel.readString();
    }

    //public Vector<Vector<String>> toVector(){
    public TripleList toTripleList(){
        Log.d("SmartSpaceService", "Start collecting information");
        //Vector<Vector<String>> resultVector = new Vector<Vector<String>>();
        TripleList resultTripleList = new TripleList();

        resultTripleList.add(new Triple(user_id, "is_a", user_type));

        if ("Driver".equalsIgnoreCase(user_type)){

            resultTripleList.add(new Triple(user_id, "vehicle", user_vehicle));
        }
        // Add start point
        resultTripleList.add(new Triple(user_id, "point", user_id + "point1"));
        resultTripleList.add(new Triple(user_id + "point1", "x", start_point_x));
        resultTripleList.add(new Triple(user_id + "point1", "y", start_point_y));
        resultTripleList.add(new Triple(user_id + "point1", "date", start_point_date));
        resultTripleList.add(new Triple(user_id + "point1", "time", start_point_time));
        if ("Driver".equalsIgnoreCase(user_type)){
            resultTripleList.add(new Triple(user_id + "point1", "vacantseats", vacant_seats));

            resultTripleList.add(new Triple(user_id + "point1", "vacantitemplaces", vacant_items));
        } else {
            resultTripleList.add(new Triple(user_id + "point1", "drivebyvehicle", "FALSE"));
        }
        // Add end point
        resultTripleList.add(new Triple(user_id, "point", user_id + "point2"));
        resultTripleList.add(new Triple(user_id + "point2", "x", end_point_x));
        resultTripleList.add(new Triple(user_id + "point2", "y", end_point_y));
        resultTripleList.add(new Triple(user_id + "point2", "date", end_point_date));
        resultTripleList.add(new Triple(user_id + "point2", "time", end_point_time));
        if ("Driver".equalsIgnoreCase(user_type)){
            resultTripleList.add(new Triple(user_id + "point2", "vacantseats", vacant_seats));

            resultTripleList.add(new Triple(user_id + "point2", "vacantitemplaces", vacant_items));
        } else {
            resultTripleList.add(new Triple(user_id + "point2", "drivebyvehicle", "FALSE"));
        }

        return resultTripleList;
    }
}
